﻿using System;
using System.Collections.ObjectModel;
using QueryBuilder.Core.Common.Properties;

namespace QueryBuilder.Core.Common
{
    public class FreezableObservableCollection<T> : ObservableCollection<T>, IFreezable
    {
        public bool IsFrozan { get; set; }

        protected override void InsertItem(int index, T item)
        {
            this.CheckFreezan();
            base.InsertItem(index, item);
        }

        private void CheckFreezan()
        {
            if (this.IsFrozan)
            {
                throw new InvalidOperationException(Resources.CannotChangeFreezableCollection);
            }
        }

        protected override void ClearItems()
        {
            this.CheckFreezan();
            base.ClearItems();
        }

        protected override void MoveItem(int oldIndex, int newIndex)
        {
            this.CheckFreezan();
            base.MoveItem(oldIndex, newIndex);
        }

        protected override void RemoveItem(int index)
        {
            this.CheckFreezan();
            base.RemoveItem(index);
        }

        protected override void SetItem(int index, T item)
        {
            this.CheckFreezan();
            base.SetItem(index, item);
        }
    }
}